package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Room;
import com.service.RoomService;
import com.util.ConstUtils;
import com.util.UUIDUtils;
import com.vo.DataVO;
import com.vo.RoomVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/26 - 11:11
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    /**
     * 查询房间集合
     *
     * @param roomVo
     * @return
     */
    @RequestMapping("/list")
    public DataVO findRoomList(RoomVo roomVo) {
        PageHelper.startPage(roomVo.getPage(), roomVo.getLimit());
        PageInfo<Room> pageInfo = new PageInfo<Room>(roomService.findRoomList(roomVo));
        return new DataVO<Room>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 上传房间主图片
     * @return
     */
    /*@RequestMapping("/uploadFile")
    public Map<String,Object> uploadFile(MultipartFile attach){
        //返回至前台对应的数据格式
        Map<String,Object> maps = new HashMap<String, Object>();
        //判断是否有文件
        if(!attach.isEmpty()){
            //指定文件上传地址
            String path = ConstUtils.ROOM_MAIN_UPLOAD_PATH;
            //获取原文件名称
            String oldName = attach.getOriginalFilename();
            //获取文件后缀名
            String extension = FilenameUtils.getExtension(oldName);
            //重命名文件名称
            String newFileName = UUIDUtils.randomID() + "." + extension;
            //新建文件夹解决文件夹过多问题
            String datePath = new SimpleDateFormat("yyyyMMdd").format(new Date());
            //拼接最后的文件名
            String finalName = datePath + "/" + newFileName;
            //创建文件对象
            File destFile = new File(path,finalName);
            //判断文件夹是否存在 不存在则创建文件夹
            if(!destFile.getParentFile().exists()){
                destFile.getParentFile().mkdirs();
            }
            try {
                //保存文件至磁盘
                attach.transferTo(destFile);
                //保存相应的数据
                maps.put("code",0);
                maps.put("msg","");
                maps.put("imagePath",finalName);//图片名称，目的是给photo隐藏域赋值
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return maps;
    }*/

    /**
     * 添加房间
     *
     * @param room
     * @return
     */
    @RequestMapping("/add")
    public Map<String, Object> addRoom(Room room) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roomService.addRoom(room) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "添加成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "添加失败，请重试！");
        }
        return maps;
    }

    /**
     * 修改房间
     *
     * @param room
     * @return
     */
    @RequestMapping("/update")
    public Map<String, Object> updateRoom(Room room) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roomService.updateRoom(room) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "修改成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "修改失败，请重试！");
        }
        return maps;
    }

    /**
     * 删除房间信息
     *
     * @param roomId
     * @return
     */
    @RequestMapping("/del")
    public Map<String, Object> deleteRoom(Integer roomId, String deleteImagePath) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roomService.deleteRoom(roomId, deleteImagePath)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "删除成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "删除失败，请重试！");
        }
        return maps;
    }
}
